package czy.demo.entity;


import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

/* 实体基类，只有@MappedSuperclass注解的超类上的属性才会被映射
 * 同时添加了审计功能实体监听器
 * 这里的Version必须是javax.persistence包下的才能起作用
 */
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {

    /* 实体创建者 */
    @CreatedBy
    @ManyToOne(targetEntity = User.class,fetch = FetchType.LAZY)
    @JoinColumn(name = "create_user",referencedColumnName = "username")
    private User createUser;

    /* 实体创建时间 */
    @CreatedDate
    @Column(name = "create_date")
    private Date createDate;

    /* 最后一次更新者 */
    @LastModifiedBy
    @ManyToOne(targetEntity = User.class,fetch = FetchType.LAZY)
    @JoinColumn(name = "update_user",referencedColumnName = "username")
    private User updateUser;

    /* 最后一次更新时间 */
    @LastModifiedDate
    @Column(name = "update_date")
    private Date updateDate;

    /* 版本 */
    @Version
    @Column(name = "version")
    private Integer version;

    public User getCreateUser() {
        return createUser;
    }

    public void setCreateUser(User createUser) {
        this.createUser = createUser;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public User getUpdateUser() {
        return updateUser;
    }

    public void setUpdateUser(User updateUser) {
        this.updateUser = updateUser;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
}
